<?php
ob_start();
session_start();

function __autoload($class_name) {
include './'.$class_name . '.php';
}

function formatDate($dateString){
	if(!$dateString){
	return null;
	}
	return date_format(new DateTime($dateString),' jS M y, h:i A');
}

$orderId = $_GET['orderId'];
$email = $_GET['email'];

$dbConfig = new DBConfig(); 
$conn = $dbConfig->getConnection();
$mysqli=$conn;
$orderId = stripslashes($orderId);

$orderId = $conn->real_escape_string($orderId);
$email = stripslashes($email);
$email = $conn->real_escape_string($email);
$result = null;
$orderdetails = null;


$res = $conn->multi_query( "CALL hambaa_tshirts.retrieve_order(@nm_fname,@nm_lname,@tx_addr1,@tx_addr2,@tx_city,@tx_state,@cd_pincode,@tx_contact_no,@st_order,@dt_order,'".$email."','".$orderId."');SELECT @nm_fname,@nm_lname,@tx_addr1,@tx_addr2,@tx_city,@tx_state,@cd_pincode,@tx_contact_no,@st_order,@dt_order;" );
    if ( $res && ($result = $conn->store_result()) && ($result->num_rows > 0)) {

	  $row_cnt = $result->num_rows;
	  $orderdetails = "{\"orders\":\"true\",\"order\" :[";
      while( $row = $result->fetch_array() ) {

		$orderdetails .= "{\"id_product\":\"".$row['id_product']."\",";
		$orderdetails .= "\"nm_product\":\"".$row['nm_product']."\",";
		$orderdetails .= "\"quantity\":\"".$row['quantity']."\",";
		$orderdetails .= "\"size\":\"".$row['cd_size']."\",";
		$orderdetails .= "\"price\":\"".$row['price']."\",";
		$orderdetails .= "\"dateShipment\":\"".$row['dt_shipment']."\",";
		$orderdetails .= "\"dateDelivery\":\"".formatDate($row['dt_delivery'])."\",";
		$orderdetails .= "\"idShipping\":\"".$row['id_shipping']."\",";
		$orderdetails .= "\"statusShipping\":\"".$row['st_shipping']."\",";
		if($row_cnt == 1){
		$orderdetails .= "\"statusDelivery\":\"".$row['st_delivery']."\"}],";
		}else{
		$orderdetails .= "\"statusDelivery\":\"".$row['st_delivery']."\"},";
		}
		$row_cnt = $row_cnt - 1;	
      }
      $result->close();
      if( $conn->more_results() ) {
	  $conn->next_result();
	  $conn->next_result();
	  $result = $conn->store_result();
	  while( $row = $result->fetch_array() ) {
		$orderdetails .= "\"fname\":\"".$row['@nm_fname']."\",";
		$orderdetails .= "\"lname\":\"".$row['@nm_lname']."\",";
		$orderdetails .= "\"addr1\":\"".$row['@tx_addr1']."\",";
		$orderdetails .= "\"addr2\":\"".$row['@tx_addr2']."\",";
		$orderdetails .= "\"city\":\"".$row['@tx_city']."\",";
		$orderdetails .= "\"state\":\"".$row['@tx_state']."\",";
		$orderdetails .= "\"pincode\":\"".$row['@cd_pincode']."\",";
		$orderdetails .= "\"contactNo\":\"".$row['@tx_contact_no']."\",";
		$orderdetails .= "\"statusOrder\":\"".$row['@st_order']."\",";
		$orderdetails .= "\"dateOrder\":\"".formatDate($row['@dt_order'])."\"}";
	  }
	  }else{
	  $orderdetails = "{\"orders\":\"false\"}";
	  }	  
	  
    }else{
	$orderdetails = "{\"orders\":\"false\"}";
	}  

$dbConfig->closeConnection();

echo $orderdetails;
ob_end_flush();
?>